﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using CustomsSystem.Model;
using Maticsoft.Common;

namespace WEB.T_bill
{
    public partial class xiaoZhang : System.Web.UI.Page
    {
        CustomsSystem.BLL.BillManage billBLL = new BillManage();
        CustomsSystem.BLL.PayManage payBll = new PayManage();
        public DataSet ds;
        int sheetID;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["yingShou"] = null;
            Session["yingFu"] = null;
            this.tbTimeStart.Attributes.Add("onClick", "new WdatePicker()");
            this.tbTimeStop.Attributes.Add("onClick", "new WdatePicker()");
           
            if (!Page.IsPostBack)
            {
                if (Session["ddlOrder"] != null || Session["query"] != null)
                {
                    tbTimeStart.Text = Session["startDate"].ToString();
                    tbTimeStop.Text = Session["stopDate"].ToString();
                    if (Session["ddlOrder"] != null)
                    {
                        if (Session["ddlOrder"].ToString() == "升序")
                        {
                            ddlOrder.SelectedValue = "asc";
                        }
                    }
                    //ddlQuery.Text = Session["ddlQuery"].ToString();
                    if (Session["ddlQuery"] != null)
                    {
                        string ddlQueryText = Session["ddlQuery"].ToString();
                        switch (ddlQueryText)
                        {
                            case "客户名":
                                ddlQuery.SelectedValue = "CustomerName";
                                break;
                            case "头程提单号":
                                ddlQuery.SelectedValue = "headLadingBillNo";
                                break;
                            case "提单号":
                                ddlQuery.SelectedValue = "ladingBillNO";
                                break;
                            case "货物名称":
                                ddlQuery.SelectedValue = "goodsName";
                                break;
                            case "船名":
                                ddlQuery.SelectedValue = "shipname";
                                break;
                            case "订单号":
                                ddlQuery.SelectedValue = "PO";
                                break;
                            case "归档号":
                                ddlQuery.SelectedValue = "filingNO";
                                break;
                            case "报检单号":
                                ddlQuery.SelectedValue = "inspectionNo";
                                break;
                            case "报关单号":
                                ddlQuery.SelectedValue = "customsNo";
                                break;
                            case "委托单单号":
                                ddlQuery.SelectedValue = "a.sheetID";
                                break;
                        }
                    }
                    if (Session["query"] != null)
                    {
                        tbQuery.Text = Session["query"].ToString();
                    }
                    Session["startDate"] = null;
                    Session["stopDate"] = null;
                    Session["ddlOrder"] = null;
                    Session["ddlQuery"] = null;
                    Session["query"] = null;
                }
                else
                {
                    //默认时间为最近一月
                    DateTime A = new DateTime();
                    A = DateTime.Today;
                    tbTimeStop.Text = A.ToShortDateString();
                    tbTimeStart.Text = A.AddDays(-30).ToShortDateString();
                }
                Bind();  
            }
           
        }


        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Bind()
        {
            SheetInfoManage sheetManage = new SheetInfoManage();
           
            string strQuery = ddlQuery.SelectedValue;
            string strSql = "  1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                if (ddlQuery.SelectedIndex == 8)
                {
                    if (PageValidate.IsNumber(tbQuery.Text.Trim()))
                    {
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                    }
                    else { MessageBox.Show(this,"请输入数字"); }
                }
                else
                {
                    strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());

                }
            }
            
            //日期搜索的判断
            if (tbTimeStart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimeStop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and agencyDate between '" + tbTimeStart.Text.Trim() + "' and '" + timeStop + "'";
                }
                else
                {
                    strSql = strSql + " and agencyDate between '" + tbTimeStart.Text.Trim() + "' and '" + tbTimeStop.Text.Trim() + "'";
                }
            }
            strSql = strSql + " order by sheetID " + ddlOrder.SelectedValue; ;
            DataSet ds = new DataSet();
            ds = sheetManage.GetViewOfUnaudit(strSql);
            if (ds.Tables[0].Rows.Count < 1)
            { lbj2.Text = "在该段时间内没有数据"; }
            else
            { lbj2.Text = ""; }
            gvSheetInfo.DataSource = ds;
            gvSheetInfo.DataBind(); 
        }

        /// <summary>
        /// 排序同样绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlorder_SelectedIndexChanged2(object sender, EventArgs e)
        {
            Bind();
        }


        //跳转到销账子页面
        protected void gvSheetInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString();
            int pageIndex= gvSheetInfo.PageIndex;

            if (e.CommandName == "SelectYS")//选择了应收销账按钮
            {
                Session["startDate"] = tbTimeStart.Text;
                Session["stopDate"] = tbTimeStop.Text;
                Session["ddlOrder"] = ddlOrder.SelectedItem.Text;
                Session["ddlQuery"] = ddlQuery.SelectedItem.Text;
                if (tbQuery.Text.Trim() != "")
                {
                    Session["query"] = tbQuery.Text;
                }

                Session["yingShou"] = "应收销账";
                int id = Convert.ToInt32(gvSheetInfo.DataKeys[(int.Parse(index)-10*pageIndex)].Value);
                string feiYong = billBLL.GetSfeeAmount(id, 1).ToString();
                string shenHe = billBLL.GetAuditEmpID(id, 1).ToString();
                if (feiYong != "" && shenHe.Trim() != "0")
                {
                    Response.Redirect("XiaoZhangYeMian.aspx?ID=" + id);//页面跳转
                }
                else
                {
                    if (feiYong == "")
                    {

                        MessageBox.Show(this, "没有应收费用");
                    }
                    else
                    {
                        MessageBox.Show(this, "应收费用还没审核");
                    }
                }
            }
        }

        //点击了应付销账按钮激发的事件
        protected void gvSheetInfo_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Session["startDate"] = tbTimeStart.Text;
            Session["stopDate"] = tbTimeStop.Text;
            Session["ddlOrder"] = ddlOrder.SelectedItem.Text;
            Session["ddlQuery"] = ddlQuery.SelectedItem.Text;
            if (tbQuery.Text.Trim() != "")
            {
                Session["query"] = tbQuery.Text;
            }

            Session["yingFu"] = "应付销账";
            int id = Convert.ToInt32(gvSheetInfo.DataKeys[e.NewSelectedIndex].Value);
            string feiYong = billBLL.GetSfeeAmount(id, 2).ToString();
            string shenHe = (billBLL.GetAuditEmpID(id, 2).ToString());
            if (feiYong != "" && shenHe.Trim() != "0")
            {
                Response.Redirect("XiaoZhangYeMian.aspx?ID=" + id);//页面跳转
            }
            else
            {
                if (feiYong == "")
                {
                    MessageBox.Show(this, "没有应付费用");
                }
                else
                {
                    MessageBox.Show(this, "应付费用还没审核");
                }
            }
        }
        
        /// <summary>
        /// 允许分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
            //换页功能
            this.gvSheetInfo.PageIndex = e.NewPageIndex;
            //绑定GridView
            gvSheetInfo.DataBind();
            TextBox tb = (TextBox)gvSheetInfo.BottomPagerRow.FindControl("tbinPageNum");
            tb.Text = (gvSheetInfo.PageIndex + 1).ToString();
            Bind();
        }
        
        /// <summary>
        /// 跳页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                gvSheetInfo.DataBind();
                
                TextBox tb = (TextBox)gvSheetInfo.BottomPagerRow.FindControl("tbinPageNum");
                int num = Int32.Parse(tb.Text.Trim());

                GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                gvSheetInfo_PageIndexChanging(null, ea);
            }
            catch { MessageBox.Show(this, "请输入数字页码"); }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvSheetInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i <= gvSheetInfo.Rows.Count; i++)
                {
                    string sheetID = e.Row.Cells[0].Text;
                    string payedID = billBLL.GetPayedID(sheetID).ToString();//根据sheetID去T_bill表里获取payedID

                    string payTypeID = payBll.GetPayTypeID(sheetID).ToString();//根据sheetID去T_pay表里获取payTpyeID
                    string payTypeNum = payBll.GetPayTypeNum(sheetID).ToString();//根据sheetID去T_pay表里获取应收、应付的销账情况，2为全都销了

                    if (payTypeNum == "2")
                    {
                        Button bt = (Button)e.Row.Cells[12].FindControl("btnYingShou");
                        bt.Enabled = false;
                        bt.Text = "已销账";
                            
                        Button btn = (Button)e.Row.Cells[13].FindControl("btnYingFu");
                        btn.Enabled = false;
                        btn.Text = "已销账";
                     }

                    if (payTypeNum == "1")
                    {

                        if (payTypeID == "1")
                        {
                            Button bt = (Button)e.Row.Cells[12].FindControl("btnYingShou");
                            bt.Text = "已销账";
                            bt.Enabled = false;
                        }
                        if (payTypeID == "2")
                        {
                            Button btn = (Button)e.Row.Cells[13].FindControl("btnYingFu");
                            btn.Text = "已销账";
                            btn.Enabled = false;
                        }
                    }
                }
            }
        }
    }
}